<template>
	<xn-form-container
		:title="formData.id ? '编辑数据源配置表' : '增加数据源配置表'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="数据源名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入数据源名称" allow-clear />
			</a-form-item>
			<a-form-item label="数据库类型：" name="provider">
				<a-select v-model:value="formData.provider" placeholder="请选择数据库类型" :options="providerOptions" />
			</a-form-item>
			<a-form-item label="数据库驱动类名：" name="driverClass">
				<a-input v-model:value="formData.driverClass" placeholder="请输入数据库驱动类名" allow-clear />
			</a-form-item>
			<a-form-item label="连接地址：" name="url">
				<a-input v-model:value="formData.url" placeholder="请输入连接地址" allow-clear />
			</a-form-item>
			<a-form-item label="用户名：" name="username">
				<a-input v-model:value="formData.username" placeholder="请输入用户名" allow-clear />
			</a-form-item>
			<a-form-item label="密码：" name="password">
				<a-input v-model:value="formData.password" placeholder="请输入密码" allow-clear />
			</a-form-item>
			<a-form-item label="数据库名称：" name="databaseName">
				<a-input v-model:value="formData.databaseName" placeholder="请输入数据库名称" allow-clear />
			</a-form-item>
			<a-form-item label="模式名称：" name="schemaName">
				<a-input v-model:value="formData.schemaName" placeholder="请输入模式名称" allow-clear />
			</a-form-item>
			<a-form-item label="端口号：" name="port">
				<a-input v-model:value="formData.port" placeholder="请输入端口号" allow-clear />
			</a-form-item>
			<a-form-item label="主机地址：" name="host">
				<a-input v-model:value="formData.host" placeholder="请输入主机地址" allow-clear />
			</a-form-item>
			<a-form-item label="连接参数(JSON格式)：" name="connectionParams">
				<a-input v-model:value="formData.connectionParams" placeholder="请输入连接参数(JSON格式)" allow-clear />
			</a-form-item>
			<a-form-item label="最大连接数：" name="maxPoolSize">
				<a-input v-model:value="formData.maxPoolSize" placeholder="请输入最大连接数" allow-clear />
			</a-form-item>
			<a-form-item label="最小空闲连接数：" name="minIdle">
				<a-input v-model:value="formData.minIdle" placeholder="请输入最小空闲连接数" allow-clear />
			</a-form-item>
			<a-form-item label="空闲超时时间(秒)：" name="idleTimeout">
				<a-input v-model:value="formData.idleTimeout" placeholder="请输入空闲超时时间(秒)" allow-clear />
			</a-form-item>
			<a-form-item label="连接最大生命周期(秒)：" name="maxLifetime">
				<a-input v-model:value="formData.maxLifetime" placeholder="请输入连接最大生命周期(秒)" allow-clear />
			</a-form-item>
			<a-form-item label="连接超时时间(秒)：" name="connectionTimeout">
				<a-input v-model:value="formData.connectionTimeout" placeholder="请输入连接超时时间(秒)" allow-clear />
			</a-form-item>
			<a-form-item label="验证查询SQL：" name="validationQuery">
				<a-input v-model:value="formData.validationQuery" placeholder="请输入验证查询SQL" allow-clear />
			</a-form-item>
			<a-form-item label="代理服务器地址：" name="proxyHost">
				<a-input v-model:value="formData.proxyHost" placeholder="请输入代理服务器地址" allow-clear />
			</a-form-item>
			<a-form-item label="代理服务器端口：" name="proxyPort">
				<a-input v-model:value="formData.proxyPort" placeholder="请输入代理服务器端口" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="aiDatasourceConfigForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import aiDatasourceConfigApi from '@/api/ai/aiDatasourceConfigApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const providerOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		providerOptions.value = tool.dictList('datasourceType')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				aiDatasourceConfigApi
					.aiDatasourceConfigSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
